package com.view;

import java.awt.event.*;
import java.sql.SQLException;
import java.util.logging.*;
import javax.swing.*;
import com.controller.*;
import com.model.*;
import java.awt.Font;

public class Document_surat extends JPanel implements ActionListener{

    private JLabel jl0, jl1, jl2, jl3, jl4, type_file, letak, ruang, lemari, loker, tanggal_msk, petugas;
    private JRadioButton rb1, rb2;
    private JCheckBox cek1, cek2;
    private ButtonGroup jb,jb1;
    private JTextField textfield1, textfield2, textfield3, textfield4, fieldruang, fieldlemari, fieldloker, fieldmasuk, fieldpetugas;
    private JButton jbut1, jbut2;

    public Document_surat() {
        this.setLayout(null);
        this.setVisible(true);
        jl0 = new JLabel();
        jl0.setText("Silahkan Memilih Salah Satu Opsi Untuk Memulai");
        jl0.setFont(new Font("Alergian", 2, 15));
        jl0.setBounds(50, 25, 350, 20);
        jl0.setVisible(true);
        this.add(jl0);
        rb1 = new JRadioButton();
        rb1.setText("Surat");
        rb1.setBounds(50, 50, 90, 20);
        rb1.setVisible(true);
        this.add(rb1);
        rb2 = new JRadioButton();
        rb2.setText("Dokumen");
        rb2.setBounds(50, 70, 90, 20);
        rb2.setVisible(true);
        this.add(rb2);
        jb = new ButtonGroup();
        jb.add(rb1);
        jb.add(rb2);
        jl1 = new JLabel();
        jl1.setBounds(50, 130, 120, 20);
        this.add(jl1);
        textfield1 = new JTextField();
        textfield1.setBounds(200, 130, 250, 20);
        textfield1.setVisible(false);
        this.add(textfield1);
        jl2 = new JLabel();
        jl2.setBounds(50, 170, 120, 20);
        this.add(jl2);
        textfield2 = new JTextField();
        textfield2.setBounds(200, 170, 250, 20);
        textfield2.setVisible(false);
        this.add(textfield2);
        jl3 = new JLabel();
        jl3.setBounds(50, 210, 120, 20);
        this.add(jl3);
        textfield3 = new JTextField();
        textfield3.setBounds(200, 210, 250, 20);
        textfield3.setVisible(false);
        this.add(textfield3);
        jl4 = new JLabel();
        jl4.setBounds(50, 250, 120, 20);
        this.add(jl4);
        textfield4 = new JTextField();
        textfield4.setBounds(200, 250, 250, 20);
        textfield4.setVisible(false);
        this.add(textfield4);
        jb1 = new ButtonGroup();
        type_file = new JLabel();
        type_file.setText("Type file : ");
        type_file.setBounds(50, 290, 70, 20);
        type_file.setVisible(false);
        this.add(type_file);
        cek1 = new JCheckBox();
        cek1.setText("File Scan");
        cek1.setBounds(200, 290, 100, 20);
        cek1.setVisible(false);
        jb1.add(cek1);
        this.add(cek1);
        cek2 = new JCheckBox();
        cek2.setText("File Dokumen");
        cek2.setBounds(200, 310, 150, 20);
        cek2.setVisible(false);
        jb1.add(cek2);
        this.add(cek2);
        letak = new JLabel();
        letak.setText("Letak :");
        letak.setBounds(50, 340,100, 20);
        letak.setVisible(false);
        this.add(letak);
        ruang = new JLabel();
        ruang.setText("Ruang");
        ruang.setBounds(50, 370, 50, 20);
        ruang.setVisible(false);
        this.add(ruang);
        fieldruang = new JTextField();
        fieldruang.setBounds(200, 370, 90, 20);
        fieldruang.setVisible(false);
        this.add(fieldruang);
        lemari = new JLabel();
        lemari.setText("Lemari");
        lemari.setBounds(310,370,50,20);
        lemari.setVisible(false);
        this.add(lemari);
        fieldlemari = new JTextField();
        fieldlemari.setBounds(380, 370, 90, 20);
        fieldlemari.setVisible(false);
        this.add(fieldlemari);
        loker = new JLabel();
        loker.setBounds(490, 370, 70, 20);
        loker.setText("Loker");
        loker.setVisible(false);
        this.add(loker);
        fieldloker = new JTextField();
        fieldloker.setBounds(540, 370, 90, 20);
        fieldloker.setVisible(false);
        this.add(fieldloker);
        tanggal_msk = new JLabel();
        tanggal_msk.setText("Tgl Masuk Berkas");
        tanggal_msk.setBounds(50, 410, 150, 20);
        tanggal_msk.setVisible(false);
        this.add(tanggal_msk);
        fieldmasuk = new JFormattedTextField();
        fieldmasuk.setBounds(200, 410, 150, 20);
        fieldmasuk.setVisible(false);
        this.add(fieldmasuk);
        petugas = new JLabel();
        petugas.setText("Petugas Penerima");
        petugas.setBounds(50, 450, 150, 20);
        petugas.setVisible(false);
        this.add(petugas);
        fieldpetugas = new JTextField();
        fieldpetugas.setBounds(200, 450, 150, 20);
        fieldpetugas.setVisible(false);
        this.add(fieldpetugas);
        rb1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (rb1.isSelected()) {
                    rb2.setEnabled(false);
                    jl1.setText("No Surat");
                    jl2.setText("Perihal Surat");
                    jl3.setText("Asal Surat");
                    jl4.setText("Penerima Surat");
                    textfield1.setVisible(true);
                    textfield2.setVisible(true);
                    textfield3.setVisible(true);
                    textfield4.setVisible(true);
                    type_file.setVisible(true);
                    cek1.setVisible(true);
                    cek2.setVisible(true);
                    letak.setVisible(true);
                    ruang.setVisible(true);
                    fieldruang.setVisible(true);
                    lemari.setVisible(true);
                    fieldlemari.setVisible(true);
                    loker.setVisible(true);
                    fieldloker.setVisible(true);
                    tanggal_msk.setVisible(true);
                    fieldmasuk.setVisible(true);
                    petugas.setVisible(true);
                    fieldpetugas.setVisible(true);
                    jbut1.setVisible(true);
                    jbut2.setVisible(true);
                    jl4.setVisible(true);
                    jl3.setVisible(true);
                    jl2.setVisible(true);
                    jl1.setVisible(true);
                }
            }
        });
        rb2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if (rb2.isSelected()) {
                    rb1.setEnabled(false);
                    jl1.setText("No Dokumen");
                    jl2.setText("Perihal Dokumen");
                    jl3.setText("Asal Dokumen");
                    jl4.setText("Penerima Dokumen");
                    textfield1.setVisible(true);
                    textfield2.setVisible(true);
                    textfield3.setVisible(true);
                    textfield4.setVisible(true);
                    type_file.setVisible(true);
                    cek1.setVisible(true);
                    cek2.setVisible(true);
                    letak.setVisible(true);
                    ruang.setVisible(true);
                    fieldruang.setVisible(true);
                    lemari.setVisible(true);
                    fieldlemari.setVisible(true);
                    loker.setVisible(true);
                    fieldloker.setVisible(true);
                    tanggal_msk.setVisible(true);
                    fieldmasuk.setVisible(true);
                    petugas.setVisible(true);
                    fieldpetugas.setVisible(true);
                    jbut1.setVisible(true);
                    jbut2.setVisible(true);
                    jl4.setVisible(true);
                    jl3.setVisible(true);
                    jl2.setVisible(true);
                    jl1.setVisible(true);
                }
            }
        });
        jbut1 = new JButton();
        jbut1.setText("SIMPAN");
        jbut1.setBounds(400, 490, 100, 20);
        jbut1.setVisible(false);
        jbut1.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if(rb1.isSelected()){
                    String nosurat=textfield1.getText();
                    String perihal = textfield2.getText();
                    String asalsurat = textfield3.getText();
                    String penerima = textfield4.getText();
                    String ruang = fieldruang.getText();
                    String lemari = fieldlemari.getText();
                    String loker = fieldloker.getText();
                    String tgl_msk = fieldmasuk.getText();
                    String petugas = fieldpetugas.getText();
                    Surat surat=new Surat();
                    if(textfield1.getText()!=null && textfield2.getText()!=null && textfield3.getText()!=null &&
                            textfield4.getText()!=null && fieldruang.getText()!=null && fieldlemari.getText()!=null
                            && fieldloker!=null && fieldmasuk.getText()!=null && fieldpetugas.getText()!=null 
                            ){
                        surat.setNomorSurat(nosurat);
                        surat.setAsalSurat(asalsurat);
                        surat.setPenerimaSurat(penerima);
                        surat.setPerihal(perihal);
                        surat.setLetakRuang(ruang);
                        surat.setLetakLemari(lemari);
                        surat.setLetakLoker(loker);
                        surat.setNamaPenerima(petugas);
                        surat.settanggaMasuk(tgl_msk);
                        if(cek1.isSelected()){
                            surat.setFilescan("File scan");
                        }else{
                            surat.setFilescan("Dokumen");
                        }
                        try {
                            PengendaliPenerimaSuratDokumen.tambahTabelSurat(surat);
                            JOptionPane.showMessageDialog(null, "Data tersimpan");
                        } catch (SQLException ex) {
                            Logger.getLogger(Document_surat.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }else{
                    String nodokumen=textfield1.getText();
                    String perihal = textfield2.getText();
                    String asaldokumen = textfield3.getText();
                    String penerima = textfield4.getText();
                    String ruang = fieldruang.getText();
                    String lemari = fieldlemari.getText();
                    String loker = fieldloker.getText();
                    String tgl_msk = fieldmasuk.getText();
                    String petugas = fieldpetugas.getText();
                    Dokumen dokumen = new Dokumen();
                    if(textfield1.getText()!=null && textfield2.getText()!=null && textfield3.getText()!=null &&
                            textfield4.getText()!=null && fieldruang.getText()!=null && fieldlemari.getText()!=null
                            && fieldloker!=null && fieldmasuk.getText()!=null && fieldpetugas.getText()!=null
                            ){
                        dokumen.setnomorDokumen(nodokumen);
                        dokumen.setPerihal(perihal);
                        dokumen.setasalDokumen(asaldokumen);
                        dokumen.setpenerimalDokumen(petugas);
                        dokumen.setLetakRuang(ruang);
                        dokumen.setLetakLemari(lemari);
                        dokumen.setLetakLoker(loker);
                        dokumen.setNamaPenerima(petugas);
                        dokumen.setTanggalTerima(tgl_msk);
                        dokumen.setNamaPenerima(penerima);
                        if(cek1.isSelected()){
                            dokumen.setFilescan("File scan");
                        }else{
                            dokumen.setFilescan("Dokumen");
                        }
                        try {
                            PengendaliPenerimaSuratDokumen.tambahTabelDokumen(dokumen);
                            JOptionPane.showMessageDialog(null, "Data tersimpan");
                        } catch (SQLException ex) {
                            Logger.getLogger(Document_surat.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }
        });
        this.add(jbut1);
        jbut2 = new JButton();
        jbut2.setText("RESET");
        jbut2.setBounds(550, 490, 100, 20);
        jbut2.setVisible(false);
        this.add(jbut2);
        jbut2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(JOptionPane.showConfirmDialog(null, "Data field akan hilang")==0){
                rb1.setEnabled(true);
                rb2.setEnabled(true);
                textfield1.setVisible(false);
                textfield1.setText(null);
                textfield2.setVisible(false);
                textfield2.setText(null);
                textfield3.setVisible(false);
                textfield3.setText(null);
                textfield4.setVisible(false);
                textfield4.setText(null);
                type_file.setVisible(false);
                cek1.setVisible(false);
                cek1.setSelected(false);
                cek2.setVisible(false);
                cek2.setSelected(false);
                letak.setVisible(false);
                ruang.setVisible(false);
                fieldruang.setVisible(false);
                fieldruang.setText(null);
                lemari.setVisible(false);
                fieldlemari.setVisible(false);
                fieldlemari.setText(null);
                loker.setVisible(false);
                fieldloker.setVisible(false);
                fieldloker.setText(null);
                tanggal_msk.setVisible(false);
                fieldmasuk.setVisible(false);
                fieldmasuk.setText(null);
                petugas.setVisible(false);
                fieldpetugas.setVisible(false);
                fieldpetugas.setText(null);
                jbut1.setVisible(false);
                jbut2.setVisible(false);
                jl4.setVisible(false);
                jl3.setVisible(false);
                jl2.setVisible(false);
                jl1.setVisible(false);
                jb1.clearSelection();
                jb.clearSelection();
            }
            }
        });
    }
    public void actionPerformed(ActionEvent e) {
    }
}
